const {
  MemberModel
} = require('../../../utils/member-model.js');
var memberModel = new MemberModel();
const app = getApp()
const {
  Config
} = require('../../../utils/config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    authShow: false,
    islogin: false,
    nickName: '',
    gameUrl: "",
    shopId: '',
    shopName: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    let that = this
    let shopInfo = wx.getStorageSync('shopInfo')
    that.setData({
      shopId: shopInfo.shopId,
      shopName: shopInfo.shopName
    },function(){
      app.getInfo().then(data => {
        if (data.data.login) {
          that.setData({
            islogin: data.data.login
          })
          that.getUserInfo()
        } else {
          that.setData({
            islogin: data.data.login,
            authShow: true
          })
        }
      })
    })
  },

  // 授权成功的回调
  loginSucceed: function () {
    let that = this
    app.getInfo().then(data => {
      that.setData({
        isLogin: data.data.login
      }, function () {
        that.getUserInfo()
      })
    })
  },
  /**
   *微信会员信息
   */
  getUserInfo: function () {
    memberModel.userInfo({
      merchantId: wx.getStorageSync('merchantId')
    }).then(data => {
      let url = Config.gameUrl+'?shopId=' + this.data.shopId + '&shopName=' + encodeURIComponent(this.data.shopName) + '&token=' + wx.getStorageSync('token') + '&merchantId=' + wx.getStorageSync('merchantId')+'&r='+new Date().getTime()
      this.setData({
        nickName: data.nickname || '',
        gameUrl: url
      })
      console.log(url)
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let activityName = '超好玩,' + this.data.nickName + '请您来吃瓜！'
    return {
      title: activityName || "",
      imageUrl: 'https://oss.kiscloud.net/wechat/images/share_watermelon.png',
      path: `pages/games/watermelon/index`,
    }
  }
})